﻿namespace AnyOffice.DAL
{
    using AnyOffice.Model;
    using Microsoft.ApplicationBlocks.Data;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class FlowSignDao
    {
        public bool Delete(SqlConnection conn, int signId)
        {
            string commandText = "DELETE FROM [dbo].[Flow_Sign] WHERE [SignId] = @SignId";
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@SignId", SqlDbType.Int) };
            commandParameters[0].Value = signId;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, commandText, commandParameters);
            return true;
        }

        public int Insert(SqlConnection conn, FlowSignData data)
        {
            string commandText = "INSERT INTO [dbo].[Flow_Sign] ( [SignNo], [SignName], [SignMemo] ) VALUES ( @SignNo, @SignName, @SignMemo ) SELECT SCOPE_IDENTITY() AS RetValue";
            SqlParameter[] commandParameters = new SqlParameter[3];
            commandParameters[0] = new SqlParameter("@SignNo", SqlDbType.Int);
            commandParameters[0].Value = data.SignNo;
            commandParameters[1] = new SqlParameter("@SignName", SqlDbType.NVarChar, 20);
            commandParameters[1].Value = data.SignName;
            commandParameters[2] = new SqlParameter("@SignMemo", SqlDbType.NText);
            if (data.SignMemoIsNull)
            {
                commandParameters[2].Value = DBNull.Value;
            }
            else
            {
                commandParameters[2].Value = data.SignMemo;
            }
            object obj2 = SqlHelper.ExecuteScalar(conn, CommandType.Text, commandText, commandParameters);
            if (obj2 != null)
            {
                return Convert.ToInt32(obj2);
            }
            return -1;
        }

        public DataTable SelectAll(SqlConnection conn)
        {
            string commandText = "SELECT [SignId], [SignNo], [SignName], [SignMemo] FROM [dbo].[Flow_Sign] ORDER BY [SignNo] ASC";
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText);
            set.Tables[0].TableName = "Flow_Sign";
            return set.Tables[0];
        }

        public DataTable SelectBySignId(SqlConnection conn, int signId)
        {
            string commandText = "SELECT [SignId], [SignNo], [SignName], [SignMemo] FROM [dbo].[Flow_Sign] WHERE [SignId] = @SignId";
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@SignId", SqlDbType.Int) };
            commandParameters[0].Value = signId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText, commandParameters);
            set.Tables[0].TableName = "Flow_Sign";
            return set.Tables[0];
        }

        public bool Update(SqlConnection conn, FlowSignData data)
        {
            string commandText = "UPDATE [dbo].[Flow_Sign] SET [SignNo] = @SignNo, [SignName] = @SignName, [SignMemo] = @SignMemo WHERE [SignId] = @SignId";
            SqlParameter[] commandParameters = new SqlParameter[4];
            commandParameters[0] = new SqlParameter("@SignId", SqlDbType.Int);
            commandParameters[0].Value = data.SignId;
            commandParameters[1] = new SqlParameter("@SignNo", SqlDbType.Int);
            commandParameters[1].Value = data.SignNo;
            commandParameters[2] = new SqlParameter("@SignName", SqlDbType.NVarChar, 20);
            commandParameters[2].Value = data.SignName;
            commandParameters[3] = new SqlParameter("@SignMemo", SqlDbType.NText);
            if (data.SignMemoIsNull)
            {
                commandParameters[3].Value = DBNull.Value;
            }
            else
            {
                commandParameters[3].Value = data.SignMemo;
            }
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, commandText, commandParameters);
            return true;
        }
    }
}

